// pages/receiveInsuranceNew/receiveInsuranceNew.js
const util = require('../../utils/util.js');
import * as utils from '../../utils/util.js';
import request from '../../utils/http.js';
import upload from '../../utils/upload.js'
import Toast from '@vant/weapp/toast/toast';
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLogin:false,
    babybirthdays:'',
    appnt: { //投保人信息
      address: '',
      cellphone: '',
      message: '',
      certInvalidDate: '请选择证件有效日期',
      customerName: '',
      certiCode: '',
      verifyShow:false
    },
    certiCodeError: '',
    insureds: { //被投保人
      birthday: '请选择出生日期',
      gender: 0,
      certInvalidDate: '请选择证件有效日期',
      certiType: '4',
      certiTypeName:'户口本',
      customerName: '',
      certiCode: '',
    },
    valDate: '',
    birthdayshow: false,
    birthdayType: 0, //0  投保人证件有效期  1 被投保人证件有效日期  2 被投保人出生日期
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    maxDate: new Date().getTime(),
    showJob: false,
    gendershow: false,
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } else if (type === 'month') {
        return `${value}月`;
      } else {
        return `${value}日`;
      }
    },
    genderList: [{
      name: '男',
      value: 0
    },
      {
        name: '女',
        value: 1
      }
    ],
    areaList: [], //省市区列表
    certiTypeList: [],//证件类型
    babyId: '',
    babySex: '男',
    repation: '父母',
    butMsg: '获取验证码',
    times:60,
    isDisabled:false,
    //校验字段
    nameError: '投保人姓名未填写，请重新确认',
    nameError1: '被保人姓名未填写，请重新确认',
    codeError: '投保人身份证号未填写，请重新确认',
    codeError1: '被投保人证件号未填写，请重新确认',
    phoneError: '',
    codeError2:'',
    isEighteen: false,//是否满18周岁   true 满18 周岁 false 未满18周岁
    isAgeC: false,//年龄差是否满18周岁   true 满18  false 未满18
    isAgeSame:false,
    show:false,
    code:'',
    codeImage:'',
    systemInfo: '',//判断是IOS或者Android查看
    pdfUrl: '',
    longStatus: false,//证件有效期是否长期有效
    disabled: false,
    isLongStatus: true,
    isOkOld: true,
    isOkYoung: true,
    msgCount: {},
    wrongMsg:[],
    currentDay: util.format(new Date()),
    currentTime: util.formatTimes(new Date()),
  },
  submit() {
    this.setData({
      disabled: true
    })
    console.log('lllll---' + this.data.isOkYoung)
    let appnt1 = this.data.appnt
    let insureds1 = this.data.insureds
    if (this.data.nameError != '') {
      Toast(this.data.nameError)
      this.setData({
        disabled: false
      })
      return;
    } else if (this.data.codeError != '') {
      Toast(this.data.codeError)
      this.setData({
        disabled: false
      })
      return;
    } else if (appnt1.certInvalidDate == '请选择证件有效日期' && !this.data.longStatus) {
      Toast('投保人证件有效日期未填写，请重新确认')
      this.setData({
        disabled: false
      })
      return;
    }else if (this.data.phoneError != '') {
      Toast(this.data.phoneError)
      this.setData({
        disabled: false
      })
      return;
    } else if (appnt1.message == '') {
      Toast('投保人手机号验证码未填写，请重新确认')
      this.setData({
        disabled: false
      })
      return;
    }
    else if (this.data.nameError1 != '') {
      Toast(this.data.nameError1)
      this.setData({
        disabled: false
      })
      return;
    } else if (insureds1.certiType== '') {
      Toast('请选择被保人的证件类型')
      this.setData({
        disabled: false
      })
      return;
    } else if (this.data.codeError1 != '') {
      Toast(this.data.codeError1)
      this.setData({
        disabled: false
      })
      return;
    } else if (this.data.isEighteen) {
      Toast('投保人必须满18周岁!')
      this.setData({
        disabled: false
      })
      return;
    } 
    // else if (!this.data.isOkOld && !this.data.longStatus) {
    //   Toast('请选择有效的投保人证件有效期')
    //   this.setData({
    //     disabled: false
    //   })
    //   return;
    // } 
    else if (insureds1.birthday == '请选择出生日期' && insureds1.certiType=='7') {
      Toast('请选择出生日期')
      this.setData({
        disabled: false
      })
      return;
    }  else if (this.data.isAgeC) {
      Toast('投保人和被保人年龄差必须大于18周岁！')
      this.setData({
        disabled: false
      })
      return;
    }
    // else if (this.data.insureds.certiType == '0' && insureds1.certInvalidDate == '请选择证件有效日期') {
    //   Toast('请选择被保人的证件有效日期')
    //   this.setData({
    //     disabled: false
    //   })
    //   return;
    // } else if (!this.data.isOkYoung) {
    //   console.log('请选择有效的被保人证件有效期')
    //   Toast('请选择有效的被保人证件有效期')
    //   this.setData({
    //     disabled: false
    //   })
    //   return;
    // }
    // if (this.data.longStatus) {//投保人证件有效期选择长期有效
    //   appnt1.certInvalidDate = ''
    // }
    if (insureds1.certiType == 0 || insureds1.certiType == 4) {
      insureds1.gender = ''
      insureds1.birthday = ''
    }
    var appnt = {
      cellphone: appnt1.cellphone,
      certInvalidDate: appnt1.certInvalidDate,
      customerName: appnt1.customerName,
      certiCode: appnt1.certiCode,
    }

    var insureds = {
      birthday: insureds1.birthday,
      gender: insureds1.gender,
      certInvalidDate: insureds1.certiType == '0'? insureds1.certInvalidDate : '',
      certiType: insureds1.certiType,
      customerName: insureds1.customerName,
      certiCode: insureds1.certiCode,
    }
    var data1 = {
      appnt: appnt,
      insureds: insureds,
    }
    var data = {
      insureInfo: JSON.stringify(data1),
      babyId: this.data.babyId,
      cellphone: appnt1.cellphone,
      message: appnt1.message,
      valDate: this.data.valDate,
    }
    console.log(data)
    console.log(JSON.stringify(data1))
    utils.HttpRequst(true, "/baby-cloud-base/babyInsurance/insure", "post", data, (res) => {
      if (res.data.code == 200) {
        this.setData({
          disabled: true
        })
        Toast("领取成功")
        wx.navigateTo({
          url: '/pages/receiveResults/receiveResults?data=' + encodeURIComponent(JSON.stringify(res.data.data)),
        })
      } else if (res.data.code == 500){
        this.setData({
          disabled: false
        })
        Toast(res.data.message)
      } else if (res.data.code == 501){
        this.setData({
          disabled: false
        })
        wx.navigateTo({
          url: '/pages/receiveResults/receiveResults?data=' + encodeURIComponent(JSON.stringify(res.data)),
        })
      } else if (res.data.code == 505) {
        let wrong = {
          day: this.data.currentTime
        }
        let list = this.data.wrongMsg
        list.push(wrong)
        this.setData({
          wrongMsg: list,
          disabled: false
        })
        wx.setStorageSync('wrongMsg', list)
        Toast(res.data.message)
      }
    })
  },
  //获取验证码按钮
  getSms() {
    if(!this.data.isDisabled){
      if (this.data.appnt.cellphone != '') {
        if (!utils.checkPhone(this.data.appnt.cellphone)) {
          Toast('请正确的手机号')
        } else {
          let isWrong = false
          if (this.data.wrongMsg.length == 4) {
            let one = this.data.wrongMsg[0]
            let four = this.data.wrongMsg[3]
            console.log(util.timeDifference(one, four))
            if (util.timeDifference(one, four) < 10) {
              isWrong = true
            } else {
              isWrong = false
              let arr = this.data.wrongMsg.slice(1)
              this.setData({
                wrongMsg: arr
              })
              wx.setStorageSync('wrongMsg', arr)
            }
          }
          //判断今天是否已经获取5次验证码
          if (this.data.msgCount.count && this.data.msgCount.count > 5 && this.data.msgCount.day == this.data.currentDay) {
            this.getCodeImage()
            this.setData({
              show: true
            })
          } else {//前5次之间获取验证码
            let code = this.data.appnt.cellphone
            var data = {
              phone: this.data.appnt.cellphone
            }
            this.getPhoneMsg(data)
          }
        }
      } else {
        Toast('请输入手机号')
      }
    }

  },
  // 关闭图片验证码弹框
  closeDia() {
    this.setData({
      show: false
    })
  },
  //确认图片验证码
  confirmCode() {
    if (this.data.codeError2 == '' && this.data.code != '') {
      let code = this.data.appnt.cellphone
      var data = {
        phone: this.data.appnt.cellphone,
        imageVerifyCode: this.data.code
      }
      this.getPhoneMsg(data)
      this.setData({
        show: false,
        code: ''
      })
    } else {
      this.setData({
        show: true
      })
      Toast(this.data.codeError2)
    }
  },
  //获取图片验证码图片
  getCodeImage() {
    var phone = this.data.appnt.cellphone
    request({
      url: '/baby-cloud-system/authService/getImageVerification',
      method: "GET",
      resType: "arraybuffer",
      data: {
        phone
      }
    }).then(res => {
      let data = wx.arrayBufferToBase64(res.data)
      this.setData({
        codeImage: data
      })
    })
  },
  checkCode2(value) {
    let code = value.detail.value
    this.setData({
      code: code
    })
    console.log(code)
    let tip = ""
    if (code == '') {
      tip = '请输入图片验证码'
      Toast(tip)
    } else if (code.length < 4) {
      tip = '请输入正确的图片验证码'
      Toast(tip)
    } else {
      tip = ''
    }
    this.setData({
      codeError2: tip
    })
  },
  getPhoneMsg(data) {
    utils.HttpRequst(false, "/baby-cloud-message/verifyCode/getMsgCode", "post", data, (res) => {
      if (res.data.code == 200) {
        let msgcount = {}
        if (wx.getStorageSync('msgCount')) {
          msgcount = wx.getStorageSync('msgCount')
          if (msgcount.day == this.data.currentDay) {
            msgcount.count += 1
          } else {
            msgcount.day = this.data.currentDay
            msgcount.count = 1
          }
        } else {
          msgcount.day = this.data.currentDay
          msgcount.count = 1
        }
        this.setData({
          msgCount: msgcount
        })
        wx.setStorageSync('msgCount', msgcount)
        console.log(msgcount)
        Toast('验证码已发送,请耐心等待!')
        this.countdown()
      } else if (data.imageVerifyCode) {
        Toast('图片验证码输入错误')
        this.getCodeImage()
        this.setData({
          code: ''
        })
      }
    })
  },
  //60秒倒计时
  countdown() {
    var that = this;
    var times = that.data.times;
    var interval;
    that.setData({
      butMsg: times + '秒',
      isDisabled: true,
    })
    interval = setInterval(function () {
      that.setData({
        butMsg: (times - 1) + ' 秒'
      })
      times--;
      if (times <= 0) {
        clearInterval(interval)
        that.setData({
          butMsg: '重新获取',
          times: 60,
          isDisabled: false
        })
      }
    }, 1000)
  },

  msgInput(e) {
    let codes = e.detail.value
    let code = "appnt.message"
    this.setData({
      [code]: codes
    })
  },
  babyGender() { //打开宝宝性别弹窗
    this.setData({
      gendershow: true
    })
  },
  babyGenClose() { //关闭宝宝性别弹窗
    this.setData({
      gendershow: false
    })
  },
  genderCancel() { //取消性别选择弹窗
    this.setData({
      gendershow: false
    })
  },
  genderConfirm(e) { //性别选择弹窗确认按钮
    console.log(e)
    let sex = "insureds.gender"
    this.setData({
      babySex: e.detail.value.name,
      [sex]: e.detail.value.value,
      gendershow: false
    })
  },
  //投保人姓名
  checkName(e) {
    let name = e.detail.value
    let names = "appnt.customerName"
    this.setData({
      [names]: name
    })
    let tip = ''
    if (name == "") {
      tip = '请输入投保人姓名'
      Toast(tip)
    } else if (!utils.checkBabyName(name)) {
      tip = '投保人姓名为2-20个由汉字和全角点号·组成的字符'
      Toast(tip)
    } else {
      tip = ''
    }
    this.setData({
      nameError: tip
    })
    console.log(this.data.appnt.customerName)
  },
  //被投保人姓名
  checkName1(e) {
    let name = e.detail.value
    let names = "insureds.customerName"
    this.setData({
      [names]: name
    })
    let tip = ''
    if (name == "") {
      tip = '请输入被投保人姓名'
      Toast(tip)
    } else if (!utils.checkBabyName(name)) {
      tip = '被投保人姓名由2-20个汉字，英文和全角点组成'
      Toast(tip)
    } else {
      tip = ''
    }
    this.setData({
      nameError1: tip
    })
  },
  //校验投保人身份证
  checkCode(e) {
    let code = e.detail.value
    code = code.toUpperCase()
    let codes = "appnt.certiCode"
    this.setData({
      [codes]: code
    })
    let isEighteen = false
    let tip = ''
    if (code == "") {
      tip = '投保人身份证号未填写，请重新确认'
      Toast(tip)
    } else if (code.length != 15 && code.length != 18) {
      tip = '请输入完整的身份证号'
      Toast(tip)
    } else if (code.length==15 &&!utils.idCard(code)) {
      tip = '投保人身份证号格式不正确'
      Toast(tip)
    } else if (code.length == 18 && !utils.idCardInterval(code)) {//18位身份证 1900-2099年
      tip = '投保人身份证号格式不正确'
      Toast(tip)
    } else {
      tip = ''
      let old = ''
      if (code.length == 18) {
        //是否满18周岁   true 满18 周岁 false 未满18周岁
        old = code.substring(6, 10) + '-' + code.substring(10, 12) + '-' + code.substring(12, 14)
      } else if (code.length == 15) {
        old = "19" + code.substring(6, 8) + "-" + code.substring(8, 10) + "-" + code.substring(10, 12);
      }
      //输入保人身份证号之后，校验是否满18周岁
      let age = util.getAge(old)
      if (age < 18) {
        isEighteen = true
        Toast('投保人必须满18周岁！')
      } else {
        let ageDate = old.substring(0, 4)
        let valYear = Number(this.data.currentDay.substring(0, 4))
        let valDate=''
        if(age > 17 && age < 26){//16-25   8年
          valYear+=8
          valDate = valYear + this.data.currentDay.substring(4, 10)
        } else if (age > 26 && age < 46){//26-45   16年
          valYear += 16
          valDate = valYear + this.data.currentDay.substring(4, 10)
        }else{
          valDate='9999-12-31'
        }
        console.log(age)
        console.log('有效日期')
        // Toast('投保人有效日期' + valDate)
        console.log(valDate)
        let cerate = 'appnt.certInvalidDate'
        this.setData({
          [cerate]: valDate
        })
      //   isEighteen = false
      //   //校验投保人
      //   let flag = false
      //   if (age > 45) {//年龄>45 不能选择长期有效
      //     flag = false

      //   } else {
      //     flag = true
      //     this.setData({
      //       longStatus: false
      //     })
      //   }
      //   if (this.data.appnt.certInvalidDate) {
      //     let dateStr = this.data.appnt.certInvalidDate
      //     let old = ''
      //     if (code.length == 18) {
      //       old = code.substring(6, 10) + '-' + code.substring(10, 12) + '-' + code.substring(12, 14)
      //     } else if (code.length == 15) {
      //       old = "19" + code.substring(6, 8) + "-" + code.substring(8, 10) + "-" + code.substring(10, 12);
      //     }
      //     //投保人年龄
      //     let age = util.getAge(old)
      //     let isOkOld = true
      //     if (new Date(utils.formatDate(new Date())) > new Date(dateStr)) {
      //       Toast('请选择有效的投保人证件有效期')
      //       isOkOld = false
      //     } else {
      //       let incalid = util.getAge(utils.formatDate(new Date())) - util.getAge(dateStr)
      //       if (age > 17 && age < 26) {//18-25岁 有效期 小于 今天+10年
      //         if (incalid > 10 || incalid < 0) {
      //           isOkOld = false
      //           Toast('请选择有效的投保人证件有效期')
      //         }
      //       } else if (age > 25 && age < 46) {//26-45岁 有效期 小于 今天+20年
      //         if (incalid > 20 || incalid < 0) {
      //           isOkOld = false
      //           Toast('请选择有效的投保人证件有效期')
      //         }
      //       }
      //     }
      //     this.setData({
      //       isOkOld: isOkOld
      //     })
      //   }
      //   this.setData({
      //     isLongStatus: flag
      //   })
      }
      this.checkAges(0)
    }

    console.log(tip)
    this.setData({
      codeError: tip,
      // isEighteen: isEighteen
    })
  },
  // checkboxChange(e) {
  //   console.log(e)
  //   let isCheck = e.detail.value[0]
  //   let status = false
  //   if (isCheck == 'false') {//选择长期
  //     status = true
  //     this.setData({
  //       ['appnt.certInvalidDate']: '请选择证件有效日期'
  //     })
  //   }
  //   this.setData({
  //     longStatus: status
  //   })
  // },
  checkAges(type) {//0 投保人身份证校验    1 被投保人
    let isAgeC = false
    if (this.data.insureds.certiType == 0 || this.data.insureds.certiType == 4) {//0 身份证 4 户口本 7 出生证
      //两个身份证都存在时 检验年龄差
      if (this.data.appnt.certiCode != '' && this.data.insureds.certiCode != '') {
        let old = ''
        let code = this.data.appnt.certiCode
        if (code.length == 18) {
          //是否满18周岁   true 满18 周岁 false 未满18周岁
          old = code.substring(6, 10) + '-' + code.substring(10, 12) + '-' + code.substring(12, 14)
        } else if (code.length == 15) {
          old = "19" + code.substring(6, 8) + "-" + code.substring(8, 10) + "-" + code.substring(10, 12);
        }
        let codeY = this.data.insureds.certiCode
        let young = codeY.substring(6, 10) + '-' + codeY.substring(10, 12) + '-' + codeY.substring(12, 14)
        //投保人年龄-被投保人年龄
        let age = util.getAge(old) - util.getAge(young)
        console.log("年龄差--"+age)
        if (age < 18) {
          isAgeC = true
          Toast('投保人和被保人年龄差必须大于18周岁！')
        } else {
          isAgeC = false
         
          // if (this.data.insureds.certInvalidDate) {
          //   let dateStr = this.data.insureds.certInvalidDate
          //   //投保人年龄
          //   let youngAge = util.getAge(young)
          //   let isOkYoung = true
          //   if (new Date(utils.formatDate(new Date())) > new Date(dateStr)) {
          //     Toast('请选择有效的被保人证件有效期')
          //     isOkYoung = false
          //   } else {
          //     let incalid = util.getAge(utils.formatDate(new Date())) - util.getAge(dateStr)
          //     if (youngAge < 19 && youngAge > 15) {//16-25岁 有效期 小于 今天+10年
          //       if (incalid > 10 || incalid < 0) {
          //         isOkYoung = false
          //         Toast('请选择有效的被保人证件有效期')
          //       }
          //     } else if (youngAge < 16) {////0-15岁 有效期 小于 今天+5年
          //       if (incalid > 5 || incalid < 0) {
          //         isOkYoung = false
          //         Toast('请选择有效的被保人证件有效期')
          //       }
          //     }
          //   }
          //   this.setData({
          //     isOkYoung: isOkYoung
          //   })
          // }

        }
        this.setData({
          isAgeC: isAgeC
        })
      }
      if (this.data.insureds.certiCode != ''){
        let codeY = this.data.insureds.certiCode
        let young = codeY.substring(6, 10) + '-' + codeY.substring(10, 12) + '-' + codeY.substring(12, 14)
        //填写身份证时，有效日期的校验
        let day = 0;
        let babydays = ''
        let bir = "insureds.birthday"
        let nowDay = new Date();
        let date = new Date(young)
        day = parseInt((nowDay.getTime() - new Date(young).getTime()) / (1000 * 60 * 60 * 24)) + 1;
        console.log('出生天数--'+day)
        if (day < 28) {
          date.setDate(date.getDate() + 29);
          babydays = utils.formatDate(date);
        }
        this.setData({
          babybirthdays: day,
          valDate: babydays
        })
        if (this.data.insureds.certiType == 0){
          let ageDate = young.substring(0, 4)
          let valYear = Number(this.data.currentDay.substring(0, 4))
          let valDate = ''
          let youngAge = util.getAge(young)
          console.log(this.data.currentDay)

          console.log(youngAge)
          if (youngAge > 15 && youngAge < 19) {//16-25   8年
            valYear += 8
            valDate = valYear + this.data.currentDay.substring(4, 10)
          } else if (youngAge >= 0 && youngAge < 16) {//0-15   4年
            valYear += 4
            valDate = valYear + this.data.currentDay.substring(4, 10)
          }
          console.log('有效日期')
          // Toast('被保人有效日期' + valDate)
          console.log(valDate)
          let cerate = 'insureds.certInvalidDate'
          this.setData({
            [cerate]: valDate
          })
        } else {
          let cerate = 'insureds.certInvalidDate'
          this.setData({
            [cerate]: ''
          })
        }
        
      }

    }else{//7 出生证
      //投保人身份证 和被投保人生日 都存在时 检验年龄差
      if (this.data.appnt.certiCode != '' && this.data.insureds.birthday != '请选择出生日期') {
        let old = ''
        let code = this.data.appnt.certiCode
        if (code.length == 18) {
          //是否满18周岁   true 满18 周岁 false 未满18周岁
          old = code.substring(6, 10) + '-' + code.substring(10, 12) + '-' + code.substring(12, 14)
        } else if (code.length == 15) {
          old = "19" + code.substring(6, 8) + "-" + code.substring(8, 10) + "-" + code.substring(10, 12);
        }
        //投保人年龄-被投保人年龄
        let age = util.getAge(old) - util.getAge(this.data.insureds.birthday)
        console.log(age)
        if (age < 18) {
          isAgeC = true
          Toast('投保人和被保人年龄差必须大于18周岁！')
        } else {
          isAgeC = false
        }
        this.setData({
          isAgeC: isAgeC
        })
      }
    }
  },
  //校验被投保人身份证
  checkCode1(e) {
    let code = e.detail.value
    code = code.toUpperCase()
    let codes = "insureds.certiCode"
    this.setData({
      [codes]: code
    })
    this.checkCertiType(code, 0)
  },
  checkCertiType(code, flag) {//flag  0输入被保人身份证号时   1 选择证件类型时
    let tip = ''
    let type = this.data.insureds.certiTypeName;
    if (this.data.insureds.certiType == '') {
      tip = '请选择证件类型'
      Toast(tip)
    } else {
      if (code == "" && flag == 0) {
        tip = `请输入被保人${type}号`
        Toast(tip)
      } else if (this.data.insureds.certiType == '0') { //身份证
        if (code && !utils.idCard(code)) {
          tip = '被保人身份证号格式不正确'
          Toast(tip)
        } else {
          tip = ''
          this.checkAges(1)
        }
      } else if (this.data.insureds.certiType == '4') {//户口本
        if (code && !utils.idCard(code)) {
          tip = '被保人身份证号由18位数字和字母组成'
          Toast(tip)
        } else {
          tip = ''
          this.checkAges(1)
        }
      } else if (this.data.insureds.certiType == '7') { //出生证
        let reg = /^[a-zA-Z][0-9]{9}$/
        if (code && !reg.test(code)) {
          tip = '被投保人的出生证由一个字母+9位数字组成'
          Toast(tip)
        } else {
          tip = ''
          this.checkAges(1)
        }
      }

    }
    this.setData({
      codeError1: tip,
    })
  },
  // 投保人手机号校验
  checkPhone(e) {
    let phone = e.detail.value
    let code = "appnt.cellphone"
    this.setData({
      [code]: phone
    })
    let tip = ''
    if (phone == "") {
      tip = '请输入投保人手机号'
      Toast(tip)
    } else if (!utils.checkPhone(phone)) {
      tip = '手机号格式不正确'
      Toast(tip)
    } else {
      tip = ''
    }
    this.setData({
      phoneError: tip
    })
  },
  //修改职业
  changeJob() {
    this.setData({
      showJob: true
    })
  },
  onSelect(e) {
    console.log(e.detail)
    let data = e.detail
    let value = data.value == '4' || data.value == '7' ? false : true;
    let type = "insureds.certiType"
    let typeName = "insureds.certiTypeName"
    this.setData({
      [type]: data.value,
      [typeName]: data.name,
      showJob: false,
      verifyShow: value
    })
    this.checkCertiType(this.data.insureds.certiCode, 1)
  },
  onClose() { //职业弹窗
    this.setData({
      showJob: false
    })
  },
  changeInvalidDatey(e) { //生日弹窗
    console.log(e.currentTarget.dataset.type)
    let type = e.currentTarget.dataset.type
    if (e.currentTarget.dataset.type == '2'){
      this.setData({
        maxDate: new Date().getTime(),
        birthdayType: e.currentTarget.dataset.type,
        birthdayshow: true
      })
    } else if (type == 0) {
      if (!this.data.longStatus) {
        this.setData({
          maxDate: new Date(2100, 1, 1).getTime(),
          birthdayType: e.currentTarget.dataset.type,
          birthdayshow: true
        })
      }

    } else {
      this.setData({
        maxDate: new Date(2100, 1, 1).getTime(),
        birthdayType: e.currentTarget.dataset.type,
        birthdayshow: true
      })
    }

  },
  onClosebirthday() { //关闭生日弹窗
    this.setData({
      birthdayshow: false,
    })
  },
  //确认生日
  birthdayComfirm(e) {
    let dateStr = utils.format(new Date(e.detail));
    //计算距离出生日期几天
    console.log(dateStr)
    let nowDay = new Date();
    let date = new Date(dateStr)

    //如果小与28天显示生效日期
    let type = this.data.birthdayType
    let bir = ''
    let code=''
    // if (type == 0) { //投保人证件有效日期
    //   bir = "appnt.certInvalidDate"
    //   let old = ''
    //   code = this.data.appnt.certiCode
    //   if (code.length == 18) {
    //     old = code.substring(6, 10) + '-' + code.substring(10, 12) + '-' + code.substring(12, 14)
    //   } else if (code.length == 15) {
    //     old = "19" + code.substring(6, 8) + "-" + code.substring(8, 10) + "-" + code.substring(10, 12);
    //   }
    //   //投保人年龄
    //   let age = util.getAge(old)
    //   let isOkOld = true
    //   if (new Date(utils.formatDate(nowDay)) > new Date(dateStr)) {
    //     Toast('请选择有效的投保人证件有效期')
    //     isOkOld = false
    //   } else {
    //     let incalid = util.getAge(utils.formatDate(nowDay)) - util.getAge(dateStr)
    //     console.log(utils.formatDate(date))
    //     console.log(incalid)
    //     if (age > 17 && age < 26) {//18-25岁 有效期 小于 今天+10年
    //       if (incalid > 10 || incalid < 0) {
    //         isOkOld = false
    //         Toast('请选择有效的投保人证件有效期')
    //       }
    //     } else if (age > 25 && age < 46) {//26-45岁 有效期 小于 今天+20年
    //       if (incalid > 20 || incalid < 0) {
    //         isOkOld = false
    //         Toast('请选择有效的投保人证件有效期')
    //       }
    //     }
    //   }
    //   this.setData({
    //     isOkOld: isOkOld
    //   })
    // } else if (type == 1) { //被投保人证件有效日期
    //   bir = "insureds.certInvalidDate"
    //   code = this.data.insureds.birthday
    //   //投保人年龄
    //   let youngAge = util.getAge(code)
    //   let isOkYoung = true
    //   if (new Date(utils.formatDate(nowDay)) > new Date(dateStr)) {
    //     Toast('请选择有效的被保人证件有效期')
    //     isOkYoung = false
    //   } else {
    //     let incalid = util.getAge(utils.formatDate(nowDay)) - util.getAge(dateStr)
    //     if (youngAge < 19 && youngAge > 15) {//16-25岁 有效期 小于 今天+10年
    //       if (incalid > 10 || incalid < 0) {
    //         isOkYoung = false
    //         Toast('请选择有效的被保人证件有效期')
    //       }
    //     } else if (youngAge < 16) {////0-15岁 有效期 小于 今天+5年
    //       if (incalid > 5 || incalid < 0) {
    //         isOkYoung = false
    //         Toast('请选择有效的被保人证件有效期')
    //       }
    //     }
    //   }
    //   console.log(isOkYoung)
    //   this.setData({
    //     isOkYoung: isOkYoung
    //   })
    // } else 
    if (type == 2) { //被投保人出生日期
      let day = 0;
      let babydays = ''
      bir = "insureds.birthday"
      day = parseInt((nowDay.getTime() - date.getTime()) / (1000 * 60 * 60 * 24)) + 1;
      if (day < 28){
        date.setDate(date.getDate() + 29);
        babydays = utils.formatDate(date);
      }

      this.setData({
        babybirthdays: day,
        valDate: babydays
      })

    }
    console.log(dateStr)
    this.setData({
      [bir]: dateStr,
      birthdayshow: false,
      // babybirthdays: day,
      // valDate:babydays
    })
    this.checkAges(1)
  },
  birthdayCancel() {
    this.setData({
      birthdayshow: false
    })
  },
  viewClause() {
    wx.showLoading({
      title: '读取中...'
    })
    wx.downloadFile({
      url: 'https://yimiao-prd.e-hqins.com/pdf/bxtk.pdf',     //仅为示例，并非真实的资源
      success: function (res) {
        var Path = res.tempFilePath              //返回的文件临时地址，用于后面打开本地预览所用
        wx.openDocument({
          filePath: Path,
          success: function (res) {
            console.log('打开成功');
          }
        })
      },
      fail: function (res) {
        console.log(res);
      },
      complete(res) {
        wx.hideLoading()
      }
    })
    // new Promise((resolve,reject)=>{
    //   wx.downloadFile({
    //     url: 'https://h5.suyuan360.com/spsy/suy/temp/YUANfcl38.pdf',     //仅为示例，并非真实的资源
    //     success: function (res) {
    //       var Path = res.tempFilePath              //返回的文件临时地址，用于后面打开本地预览所用
    //       wx.openDocument({
    //         filePath: Path,
    //         success: function (res) {
    //           console.log('打开成功');
    //         },
    //         fail: function (res) {
    //           console.log(res);
    //         }
    //       })
    //     }
    //   }).then(res=>{
    //     this.setData({
    //       mask_view: true,
    //       saveQRCodeDialog: true
    //     })
    //     wx/wx.switchTab({
    //       url: '/pages/index/index',
    //     })
    //   }).catch(err=>{
    //     this.setData({
    //       mask_view: false,
    //       saveQRCodeDialog: false
    //     })
    //   })
    // })
  },
  onLoad() {
    let that = this;
    utils.HttpRequst(false, '/baby-cloud-system/dictType/listAll', 'post', {}, res => {
      if (res.data.code == 200) {
        let obj = {}
        res.data.data.forEach((item => {
          for (let p in item) {
            obj[p] = item[p]
          }
        }))
        let certiTypeList = obj["cartType"].data;
        that.setData({
          certiTypeList: certiTypeList,
        })
        wx.setStorageSync('dict', obj)
      }
    })
  },
  onShow() {
    let that = this;
    if (wx.getStorageSync('msgCount')) {
      if (wx.getStorageSync('msgCount').day==this.data.currentDay){
        that.setData({
          msgCount: wx.getStorageSync('msgCount')
        })
      }else{
        let day={
          day: this.data.currentDay,
          count:0
        }
        wx.setStorageSync('msgCount', day)
      }
    }
    if (wx.getStorageSync('wrongMsg')) {
      that.setData({
        wrongMsg: wx.getStorageSync('wrongMsg')
      })
    }
    let isLogin = wx.getStorageSync('isLogin') || app.globalData.isLogin;
    that.setData({
      isLogin: isLogin
    })
    if(!isLogin) {
      wx.navigateTo({
        url: '/pages/login/login'
      })
    }else {
      let phone = "appnt.cellphone"
      let beforeEighteenyear = new Date(utils.reduceDate('Y', utils.formatDate(new Date()), "18")).getTime()
      that.setData ({
        isLogin: isLogin,
        [phone]: wx.getStorageSync('telNum'),
        minDate: beforeEighteenyear,
        maxDate: new Date().getTime(),
        currentDate: new Date().getTime(),
      })
      //判断是IOS或者Android查看
      new Promise((resolve,reject)=>{
        wx.getSystemInfo({
          success: function (res) {
          }
        })
      }).then(res=>{
        that.setData({
          systemInfo: res,
          pdfUrl: 'https://yimiao-prd.e-hqins.com/pdf/bxtk.pdf'
        });
      }).catch(err=>{
      })
    }

  }
})
